package com.edu.javaproect.request;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
 * @author:秇稔
 * @email:2580108178@qq.com
 * @Date:2021/12/6 20:25
 * @desc：该类用于封装提供网络请求的功能代码，用于发起网络请求并将网络的数据进行返回
 **/
public class HttpUtils {
    //该函数用于实现get请求，并将请求的结果进行返回
    //path 返回请求的资源路径  return 返回请求的结果
    public static String get(String path){
        String line = "";//最开始空行
        String  result = "";//每次读取到以后都要累计起来的读取结果

        BufferedReader reader = null;
        InputStreamReader inputStreamReader = null;
        InputStream inputStream = null;
        
        try {
            //Union Resourse Location 统一资源定位
            URL url = new URL(path);
            URLConnection conn = url.openConnection();
            //进行连接
            conn.connect();

            inputStream = conn.getInputStream();

            //字符流：按行读
            inputStreamReader = new InputStreamReader(inputStream);
             reader = new BufferedReader(inputStreamReader);
//            String line = reader.readLine();//每次读一行
//            while(line != null){
//                reader.readLine();
//            }
            while ((line = reader.readLine()) != null){
                result += line;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭读取流 顺序不能调换，先开的后关，后关的先开
            try {
                if (reader != null) {
                    reader.close();
                }
                if (inputStreamReader != null){
                    inputStreamReader.close();
                }
                if (inputStream != null){
                    inputStream.close();
                }
            }catch (IOException e) {
                    e.printStackTrace();
            }
        }
        return result;
    }

    //封装java中的原生请求，并返回结果
    // path 请求的路径  header 请求头的设置 data 要发送的请求的数据 result 请求的结果
    public static String post(String path, Map<String,String> header, byte[] data) {
        OutputStream outputStream = null;
        InputStream inputStream = null;
        BufferedReader reader =null;
        String result = "";
        try {
            URL url = new URL(path);
            //强制类型转换
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);//设置允许向外输出的数据
            conn.setDoInput(true);//设置可以读取的数据
            //设置请求头消息
            //如何读取Map
            if (!header.isEmpty()) {
                //拿到所有的key
                Set<String> keys = header.keySet();
                for (String key : keys) {
                    String value = header.get(key);
                    conn.setRequestProperty(key, value);
                }
            }
            //进行连接
            conn.connect();

            //将数据发送出去
            outputStream = conn.getOutputStream();
            outputStream.write(data);//将数据写出去

            //接受响应数据
            inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
            //一行一行读
            String line = null;
            while ((line = reader.readLine())!= null){
                result += line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (inputStream != null){
                    inputStream.close();
                }
                if (outputStream != null){
                    outputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return result;
    }
}

